/**
 * Created by dale on 2017/9/13.
 */
    var myMeasue;
    var nowsBoxCode;
    var ableFlag;
    getTotal();
    var flag=true;
    var targetObj; //生成的目标target容器

    $("#iTypeId").combobox({  //商品类型层
        url:baseUrl+'ims/moblie/getTypeOther/false',
        valueField:'id',
        textField:'text',
        onSelect:function (rec) {
            var id = rec['id'];
            $('#iProdId').combobox({   //商品层
                url:baseUrl+'ims/moblie/getProds?id='+id,
                valueField:'iProdId',
                textField:'sProdName',
                onSelect:function (record) {
                    
                    $('#sMeasure').combobox({  //量词层
                        url:baseUrl+'ims/moblie/getMeasue?iProdId='+record['iProdId'],
                        valueField:'id',
                        textField:'text',
                        onSelect:function (meas) { //只,箱
                            myMeasue=meas;
                            if(flag){
                                console.log(meas['id']);
                                if(meas['id']=='只'||meas['id']=='支'||meas['id']=='个'||meas['id']=='本'||meas['id']=='包'||meas['id']=='台'||meas['id']=='张'||meas['id']=='瓶'||meas['id']=='支'){
                                    append(1,meas['id'],meas['measureType']);
                                }else if(meas['id']=='箱'||meas['id']=='袋'||meas['id']=='条'||meas['id']=='盒'){
                                    append(2,meas['id'],meas['measureType']);
                                    $('#iProdNum').numberbox('setValue', meas['num']);
                                }
                            }else {
                                targetObj.remove();
                                if(meas['id']=='只'||meas['id']=='支'||meas['id']=='个'||meas['id']=='本'||meas['id']=='包'||meas['id']=='台'||meas['id']=='张'||meas['id']=='瓶'||meas['id']=='支'){
                                    append(1,meas['id'],meas['measureType']);
                                }else if(meas['id']=='箱'||meas['id']=='袋'||meas['id']=='条'||meas['id']=='盒'){
                                    append(2,meas['id'],meas['measureType']);
                                   $('#iProdNum').numberbox('setValue', meas['num']);
                                }
                            }
                            
                        }
                    })

                }
            });
        },
        onLoadSuccess: function () {
            var val = $(this).combobox("getData");
            $(this).combobox("setValue",val[0]['id']);
        }
    });


function append($type,$measure,$measureType) {
    var content='';
    if($type==1){
        content = '<div style="margin-bottom:10px">'
                        +'<input id="sProdCode" class="easyui-textbox" label="'+$measure+'码:"  name="sProdCode" data-options="buttonText:\'扫描\',buttonIcon:\'btn-scan\',prompt:\'扫描货码\',labelPosition:\'top\'" style="width:100%">'
                +'</div>'
    }
    if($type==2){
        content = '<div style="margin-bottom:10px">'
                        +'<input id="sBoxCode" class="easyui-textbox" label="'+$measure+'码:"  name="sBoxCode" data-options="buttonText:\'扫描\',buttonIcon:\'btn-scan\',prompt:\'扫描货码\',labelPosition:\'top\'" style="width:100%">'
                    +'</div>';
        content += '<div style="margin-bottom:10px">'
                        +'<input id="iProdNum" class="easyui-numberbox" label="'+$measureType+':"  name="iProdNum" data-options="labelPosition:\'top\'"  style="width:100%">'
                    +'</div>';

    }
    if($type==3){
        content += '<div style="margin-bottom:10px">'
            +'<input id="sProdCode" class="easyui-textbox" label="'+$measure+'码:"  name="sProdCode" data-options="buttonText:\'扫描\',buttonIcon:\'btn-scan\',prompt:\'扫描货码\',labelPosition:\'top\'" style="width:100%">'
            +'</div>';
        var boxName = getMeasure($measureType,2);
        content += '<div style="margin-bottom:10px">'
            +'<input id="sBoxCode" class="easyui-textbox" label="'+boxName+'码:"  name="sBoxCode" data-options="labelPosition:\'top\'" style="width:100%">'
            +'</div>';

    }

    content +='<div style="margin-bottom:10px">';
    if($type==2){

        content +='<a id="DetailIn" class="easyui-linkbutton" data-options="iconCls:\'btn-detail\'" style="padding:5px 0px;width:100px;float: right" >'
            +'<span style="font-size:14px;">明细</span>'
            +'</a>';
    }
    if($type==3){
        content +='<a id="back" class="easyui-linkbutton" data-options="iconCls:\'icon-undo\'" style="padding:5px 0px;width:100px;float: right" >'
            +'<span style="font-size:14px;">返回</span>'
            +'</a>';
    }
    content +='<a id="saveIn" class="easyui-linkbutton" data-options="iconCls:\'btn-save\'" style="padding:5px 0px;width:100px;float: right;margin-right: 5px;">'
        +'<span style="font-size:14px;">提交</span>'
        +'</a>'
        +'</div>';

    targetObj = $(content).appendTo("#ff");
    $.parser.parse(targetObj);
    if($type == 2){
        $('#DetailIn').hide();
    }
    if($type==3){
        $('#sMeasure').combobox('disable');
        $('#iTypeId').combobox('disable');
        $('#iProdId').combobox('disable');
        $('#sBoxCode').textbox('setValue',nowsBoxCode).textbox('disable');
        $('#back').hide();
    }
    flag=false;
    return targetObj;
}
$('#ff').on('click','#DetailIn',function () {
    window.parent.setTitle('入库明细');
    ableFlag=true;
    if($('#sBoxCode').textbox('getValue')==''){
        $.messager.alert('错误','物品码为空','info');
        return false;
    }else {
        nowsBoxCode=$('#sBoxCode').textbox('getValue');
    }
    targetObj.remove();
    var id=getMeasure(myMeasue['measureType'],1);
    $('#sMeasure').combobox('setValue',id); //设置只
    targetObj.remove();//移除只创建
    append(3,id,myMeasue['measureType']);
});

$('#ff').on('click','#back',function () {
    window.parent.setTitle('入库');
    ableFlag=false;
    targetObj.remove();
    var id=getMeasure(myMeasue['measureType'],false);
    $('#sMeasure').combobox('setValue',id);
    $('#sMeasure').combobox('enable');
    $('#iTypeId').combobox('enable');
    $('#iProdId').combobox('enable');
    $('#sBoxCode').textbox('setValue',nowsBoxCode);
    $('#sBoxCode').textbox('enable');
});



$('#ff').on('click','#saveIn',function () {  //提交表单
    if(ableFlag){
        $('#sMeasure').combobox('enable');
        $('#iTypeId').combobox('enable');
        $('#iProdId').combobox('enable');
        $('#sBoxCode').textbox('enable');
    }

    $.ajax({
        url:baseUrl+'ims/moblie/saveIn',
        data:$('#ff').serialize(),
        type:'post',
        success:function (data) {
            $.messager.alert('成功',data,'info');
            getTotal();
            if($('#DetailIn')!=null){
                $('#DetailIn').show();
            }
            if(ableFlag){
                getBoxNum($('#sBoxCode').textbox('getValue'));
                $('#sMeasure').combobox('disable');
                $('#iTypeId').combobox('disable');
                $('#iProdId').combobox('disable');
                $('#sBoxCode').textbox('disable');
            }
        },
        error:function (data) {
            $.messager.alert('错误',data['responseText'],'error');
        }
    });
});


/**
 * 明细获取到箱内的boxCode
 *
 * created by dale 2017126
 */
function getBoxNum(boxCode) {
    $.ajax({
        url:baseUrl+'ims/res/getBoxNum',
        data:{boxCode:boxCode},
        type:'post',
        dataType:'json',
        success:function (data) {
            if(data['total'] == data['current']){
                $('#back').click();
                window.top.setTitle('入库');
                $.messager.alert('成功','该箱内所有平板均已导入','info');
            }else {
                window.top.setTitle('明细-剩余需导入平板'+(data['total']-data['current'])+'个');
            }
        }
    })
}

/**
 * 获取量词
 * @param measure 量词字符串
 * @param single  true or false 单个
 */
function getMeasure(measure,single) {
    var id;
    if(single==1){
        $.each(measure.split('/'),function (key,value) {
            if(meas['id']=='只'||meas['id']=='支'||meas['id']=='个'||meas['id']=='本'||meas['id']=='包'||meas['id']=='台'||meas['id']=='张'||meas['id']=='瓶'||meas['id']=='支'){
                id=value;
            }
        });
        return id;
    }else{
        $.each(measure.split('/'),function (key,value) {
            if(meas['id']=='箱'||meas['id']=='袋'||meas['id']=='条'||meas['id']=='盒'){
                id=value;
            }
        });

        return id;
    }
}

function getTotal() {
    $.ajax({
        url:baseUrl+'ims/moblie/getTotal',
        type:'post',
        success:function (data) {
            $('#text').empty();
            $('#text').append(data);
        },
        error:function (data) {

        }
    })
}


/**
 * 扫码回调
 * @type {{setResult: JSObjectHandle.setResult}}
 * created by dale 2017/9/15
 */
var JSObjectHandle={
    setResult:function (code) {
        setTimeout(function () {
            if($clickFlag === 2){
                $('#sProdCode').textbox('setValue',code).textbox({editable:false});
            }else {
                $('#sBoxCode').textbox('setValue',code).textbox({editable:false})
            }
        },0);
    }
}

$('#ff').on('click',('.textbox-button'),function () {  //textBox按钮点击事件
    var id=$(this).parent().prev().attr('id');
    if(id === 'sBoxCode'){
        $clickFlag = 1;
    }else {
        $clickFlag = 2;
    }
    // window.webkit.messageHandlers.openScanf.postMessage(null);
    window.top.openScan();
});

